 package com.gmrz.uaf.common;
 
 import com.gmrz.util.Convert;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
 public class TLVTag
 {
   private static final int MAX_DATALENGTH = 1024 * 1024 * 100;
   private static final String EMPTY_STRING = "";
   private short value = -1;
 
   public TLVTag(short t)
   {
     this.value = t;
   }
 
   public short getValue() {
     return this.value;
   }
 
   private short getDataLength(ByteBuffer buffer) {
     ByteOrder old = buffer.order();
     buffer.order(ByteOrder.LITTLE_ENDIAN);
 
     short dataLength = buffer.getShort();
     buffer.order(old);
     return dataLength;
   }
 
   public byte[] forLong(long l) {
     return ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(l).array();
   }
 
   public long parseLong(ByteBuffer buffer) throws IOException {
     short dataLength = getDataLength(buffer);
     if (dataLength != 8)
       throw new IllegalArgumentException("Invalid data length for a long value in the TLV data: " + dataLength);
     ByteOrder old = buffer.order();
     buffer.order(ByteOrder.LITTLE_ENDIAN);
 
     long retval = buffer.getLong();
     buffer.order(old);
     return retval;
   }
 
   public byte[] forShort(short s) {
     return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(s).array();
   }
 
   public short parseShort(ByteBuffer buffer) {
     short dataLength = getDataLength(buffer);
     if (dataLength != 2)
       throw new IllegalArgumentException("Invalid data length for a short value in the TLV data: " + dataLength);
     ByteOrder old = buffer.order();
     buffer.order(ByteOrder.LITTLE_ENDIAN);
 
     short retval = buffer.getShort();
     buffer.order(old);
     return retval;
   }
 
   public byte[] forBoolean(boolean b) {
     return ByteBuffer.allocate(1).put((byte)(b ? 1 : 0)).array();
   }
 
   public boolean parseBoolean(ByteBuffer buffer) {
     short dataLength = getDataLength(buffer);
     if (dataLength != 1) {
       throw new IllegalArgumentException("Invalid data length for a boolean value in the TLV data: " + dataLength);
     }
     byte b = buffer.get();
     return b == 1;
   }
 
   public byte[] forByte(byte b)
   {
     return new byte[] { b };
   }
 
   public byte parseByte(ByteBuffer buffer) {
     short dataLength = getDataLength(buffer);
     if (dataLength != 1) {
       throw new IllegalArgumentException("Invalid data length for a byte value in the TLV data: " + dataLength);
     }
     return buffer.get();
   }
 
   public byte[] forString(String str) throws UnsupportedEncodingException {
     if (str == null) {
       return new byte[0];
     }
     byte[] utf8Bytes = Convert.toUtf8(str);
     return utf8Bytes;
   }
 
   public String parseString(ByteBuffer buffer) throws UnsupportedEncodingException {
     short dataLength = getDataLength(buffer);
     if ((dataLength < 0) || (dataLength > MAX_DATALENGTH)) {
       throw new IllegalArgumentException("Invalid data length for a string value in the TLV data: " + dataLength);
     }
     if (dataLength == 0) {
       return EMPTY_STRING;
     }
     byte[] data = new byte[dataLength];
 
     buffer.get(data);
     return Convert.fromUtf8(data);
   }
 
   public byte[] forByteArray(byte[] bytes)
   {
     return bytes;
   }
 
   public byte[] parseByteArray(ByteBuffer buffer) {
     short dataLength = getDataLength(buffer);
     if ((dataLength < 0) || (dataLength > MAX_DATALENGTH))
       throw new IllegalArgumentException("Invalid data length for a byte array value in the TLV data: " + dataLength);
     byte[] retval = new byte[dataLength];
 
     buffer.get(retval);
     return retval;
   }
 
   public static short readTag(ByteBuffer buffer)
   {
     ByteOrder old = buffer.order();
     buffer.order(ByteOrder.LITTLE_ENDIAN);
     try
     {
       // 获取一个简短的值
       short value = buffer.getShort();
       return value;
     } finally {
       buffer.order(old);
     }
   }
 }
